package com.hcbd.tdwt.ui.fuda.common.activity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.view.KeyEvent;
import android.widget.RelativeLayout;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.bd.BDNotificationManager;
import com.hcbd.tdwt.ui.fuda.common.fragment.CommunicationFragment_;
import com.hcbd.tdwt.ui.fuda.common.fragment.LocationFragment_;
import com.hcbd.tdwt.ui.fuda.common.fragment.MyFragment_;

import com.hcbd.tdwt.base.AuthManger;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.BottomBar;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;


@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_main)
public class MainActivity extends BaseActivity{
    @ViewById
    RelativeLayout mainLL;
    @ViewById
    BottomBar bottomBar;
    private LocationFragment_ locationFragment_;
    private CommunicationFragment_ communicationFragment_;
    private MyFragment_ myFragment_;
    private final int  MANAGE_EXTERNAL_REQUEST_CODE=15;
    @AfterViews
    protected void init() {
        locationFragment_=new LocationFragment_();
        communicationFragment_=new CommunicationFragment_();
        myFragment_=new MyFragment_();
        mainLL.setBackground(getDrawable(R.color.white));
        bottomBar.setBackground(getDrawable(R.color.white));
        AuthManger.getAuthManger().initDialog(this);
        initBottomBar();
        manageExternalPermission();
  /*    String str="$BDTCI,1859840,1859840,2,032504,2,0,900000000000636C6F0BC4E3BAC3*4D";
        BdIcpInfo bdIcpInfo = new BdIcpInfo();
        bdIcpInfo.build2_1(str,null);*/
    }
    private void manageExternalPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            // 先判断有没有权限
            if (!Environment.isExternalStorageManager()) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                intent.setData(Uri.parse("package:" + this.getPackageName()));
                startActivityForResult(intent, MANAGE_EXTERNAL_REQUEST_CODE);
            }else{
                checkPermission();
            }
        }else{
            checkPermission();
        }
    }
    public void checkPermission(){
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, permissionRequestCode);
        }else{
            AuthManger.getAuthManger().initZipSdk();

        }
    }
    public void initBottomBar() {
        bottomBar.setContainer(R.id.fl_container)
                .setFirstChecked(0)
                .setTitleBeforeAndAfterColor("#393939", "#0160C2")
                .addItem(locationFragment_,
                        "位置", R.drawable.fuda_location_pre,
                        R.drawable.fuda_location_on)
                .addItem(communicationFragment_,
                        "通讯", R.drawable.fuda_communication_pre,
                        R.drawable.fuda_communication_on)
                .addItem(myFragment_,
                        "我的", R.drawable.fuda_my_pre,
                        R.drawable.fuda_my_on).buildTwo();

        bottomBar.setOnSwitchListener(currentFragment -> {
            if (currentFragment instanceof LocationFragment_) {
                ((LocationFragment_) bottomBar.getCurrentFragment()).onSwitch();
            }else if (currentFragment instanceof CommunicationFragment_) {
                ((CommunicationFragment_) bottomBar.getCurrentFragment()).onSwitch();
            }else if (currentFragment instanceof MyFragment_) {
                ((MyFragment_) bottomBar.getCurrentFragment()).onSwitch();
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        BDNotificationManager.getInstance().clsoeBdcom();
    }
    private long mExitTime=0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        AuthManger.getAuthManger().initZipSdk();
        if (requestCode == permissionRequestCode) {
            locationFragment_.onRequestPermissionsResult(requestCode,permissions,grantResults);
        }
    }
    @OnActivityResult(MANAGE_EXTERNAL_REQUEST_CODE)
    void manageExternalPermissionResult(){
        checkPermission();
    }
}
